<?php
//命名空间声明
namespace Admin\Controller;
//引入父类控制器
use Think\Controller;
//声明类并且继承父类
class DeptController extends BaseController{
	public function add(){
		if(IS_POST){
			$post=I('post.');
			$model=M(Dept);
			$result=$model->add($post);
			if($result){
				$this->success('添加成功',U('showList'),3);
			}else{
				$this->error('添加失败');
			}
		}else{
			$model=M('Dept');
			$data=$model->where('pid=0')->select();
			$this->assign('data',$data);
			$this->display();
		}
	}
	public function showList(){
		$model=M('Dept');
		$data=$model->field('t1.*,t2.name as deptname')->alias('t1')->join('left join oa_dept as t2 on t1.pid=t2.id')->order('sort asc')->select();
		$count=$model->count();
		// foreach($data as $key=>$value){
		// 	if($value['pid']>0){
		// 		$info=$model->find($value['pid']);
		// 		$data[$key]['deptname']=$info['name'];
		// 	}else{
		// 		$data[$key]['deptname']='顶级部门';
		// 	}
			
		// }
		load('@/tree');
		$data=getTree($data);
		$this->assign('data',$data);
		$this->assign('show',$show);
		$this->display();
	}
	public function edit(){
		if(IS_POST){
			$post=I('post.');
			$model=M('Detail');
			$result=$model->save($post);
			if ($result !== false) {
				$this->success('修改成功',U('showList'),3);
			}else{
				$this->error('修改失败');
			}
		}else{
			$id=I('get.id');
			$model=M('Detail');
			$data=$model->find($id);
			$data2=$model->where("id!=$id")->select();
			$this->assign('data',$data);
			$this->assign('data2',$data2);
			$this->display();
		}
	}
	public function del(){
		$id=I('get.id');
		$model=M('Dept');
		$result=$model->delete($id);
		if($result){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}
	public function _empty(){
		$this->display('Empty/error');
	}
}